package com.sunxu.ioc.dependecy.injection;

import com.sunxu.ioc.domain.User;
import com.sunxu.ioc.repository.UserRepository;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author 孙许
 * @version 1.0
 * @date 2020/4/11 10:21 下午
 * 依赖注入和依赖查找并不是一回事
 */
public class DependencyInjectionDemo {
    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:META-INF/dependency-injection-context.xml");
        UserRepository userRepository = (UserRepository) beanFactory.getBean("userRepository");
        System.out.println(userRepository.getBeanFactory());
        // 依赖注入
        System.out.println(userRepository.getUsers());
        // 依赖查找 (错误)
//        System.out.println(beanFactory.getBean(BeanFactory.class));
        System.out.println(beanFactory == userRepository.getBeanFactory());
        whoIsIoCContainer(userRepository, beanFactory);
        //
        ObjectFactory<ApplicationContext> objectFactory = userRepository.getObjectFactory();
        System.out.println(beanFactory == objectFactory.getObject());
    }

    private static void whoIsIoCContainer(UserRepository userRepository, BeanFactory beanFactory) {
        // 这个表达式为什么不会成立  组合方式,(表面是继承)
        System.out.println("谁是ioc容器" + (userRepository.getBeanFactory() == beanFactory));
    }

}
